package com.min.juc.utilclass.countdownlatch;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
//6辆车 3个车位停车
public class SemaphoreDemo01 {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);

        for (int i = 0; i < 6; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();  //得到
                    System.out.println(Thread.currentThread().getName() + "得到车位");
                    TimeUnit.SECONDS.sleep(2);
                    System.out.println(Thread.currentThread().getName() + "释放车位");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();  //释放
                }
            },String.valueOf(i)).start();
        }

    }
}
